package com.smsc.headend.task.engine.service.dds.impl;

import cn.hutool.core.util.StrUtil;
import com.jcraft.jsch.SftpException;
import com.smsc.headend.task.engine.ftp.config.SftpConfig;
import com.smsc.headend.task.engine.ftp.config.SftpUtils;
import com.smsc.headend.task.engine.service.dds.ScheduleDdsService;
import com.smsc.headend.task.engine.service.FeignAssetManagementService;
import com.smsc.headend.task.engine.manager.DdsFilterMdmDataManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class ScheduleDdsServiceImpl implements ScheduleDdsService {

    @Autowired
    SftpConfig sftpConfig;

    @Autowired
    FeignAssetManagementService feignAssetManagementService;

    @Autowired
    DdsFilterMdmDataManager ddsFilterMdmDataManager;

    @Autowired
    SftpUtils sftpUtils;

    @Override
    public void quartzUploadFile() {
        log.info("DataFilesUpload Start...");
        try {
            String localDir = feignAssetManagementService.getPropertyValue("DDS_SFTP_LOCALDIR");
            String remotePath = feignAssetManagementService.getPropertyValue("DDS_SFTP_REMOTE_PATH");
            String remotePathCsv = feignAssetManagementService.getPropertyValue("DDS_CSV_SFTP_REMOTE_PATH");

            localDir = StrUtil.isNotEmpty(localDir) ? localDir : sftpConfig.getLocalDir();
            remotePath = StrUtil.isNotEmpty(remotePath) ? remotePath : sftpConfig.getRemotePath();
            ddsFilterMdmDataManager.tmpFileCheck(localDir);
            sftpUtils.upLoadFiles(localDir, remotePath, remotePathCsv, sftpConfig);
        } catch (SftpException | IOException e) {
            log.error("SftpException:[{}]", new Object[]{e});
        }
        log.info("DataFilesUpload End...");
    }
}
